package org.wmq.Mapper.impl;

import com.alibaba.fastjson.JSON;
import org.wmq.Cache.LocalCache;
import org.wmq.Mapper.MySimpleMapMapper;
import org.wmq.controller.ViewController;
import org.wmq.model.Entity.MySimpleMap;

import java.io.*;
import java.util.Map;

public class MySimpleMapMapperImpl implements MySimpleMapMapper {
    public static final String MySimpleMapFilePrefix = "D:\\map_collection_data\\map_data";
    private static final String MySimpleMapFileSuffix = ".txt";

    @Override
    public MySimpleMap selectById(Long id) throws IOException {
        MySimpleMap result = LocalCache.get(id);
        if (result != null){
            return result;
        }
        String filename = MySimpleMapFilePrefix + "\\" + id
                 + MySimpleMapFileSuffix;
        File file = new File(filename);
        if (!file.exists()){
            System.out.println("文件不存在");
            return null;
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        String JsonString = new String(fileInputStream.readAllBytes());
        result = JSON.parseObject(JsonString, MySimpleMap.class);
        fileInputStream.close();
        LocalCache.add(id,result);
        return result;
    }

    @Override
    public boolean insert(MySimpleMap mySimpleMap) throws IOException {
        String filename = MySimpleMapFilePrefix + "\\"
                + mySimpleMap.getId() + MySimpleMapFileSuffix;
        File file = new File(filename);
        if (file.exists()){
            System.out.println("当前文件已经存在");
            return false;
        }
        file.createNewFile();
        String jsonString = JSON.toJSONString(mySimpleMap);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(jsonString.getBytes());
        fileOutputStream.close();
        LocalCache.add(mySimpleMap.getId(),mySimpleMap);
        ViewController.addDataToList(mySimpleMap);
        return true;
    }

    @Override
    public boolean updateById(MySimpleMap mySimpleMap) throws IOException {
        String filename = MySimpleMapFilePrefix + "\\"
                + mySimpleMap.getId() + MySimpleMapFileSuffix;
        File file = new File(filename);
        if (!file.exists()){
            System.out.println("文件不存在");
            return false;
        }
        file.delete();
        file.createNewFile();
        MySimpleMap cacheData = LocalCache.get(mySimpleMap.getId());
        cacheData.update(mySimpleMap);
        String jsonString = JSON.toJSONString(cacheData);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(jsonString.getBytes());
        fileOutputStream.close();
        ViewController.getByIndex().update(mySimpleMap);
        return true;

    }

    @Override
    public boolean deleteById(Long id) throws IOException {
        String filename = MySimpleMapFilePrefix + "\\" + id
                + MySimpleMapFileSuffix;
        File file = new File(filename);
        if (!file.exists()){
            System.out.println("文件不存在");
            return false;
        }
        file.delete();
        LocalCache.remove(id);
        ViewController.deleteByIndex();
        return true;
    }

    @Override
    public MySimpleMap select(MySimpleMap mySimpleMap) {
        Object[] array = LocalCache.getCache().entrySet().stream().filter(entry -> {
            MySimpleMap value = entry.getValue();
            boolean result = true;
            if (mySimpleMap.getName() != null && !mySimpleMap.getName().equals("")) {
                result = result && value.getName().equals(mySimpleMap.getName());
            }
            if (mySimpleMap.getMapType() != null) {
                result = result && value.getMapType().equals(mySimpleMap.getMapType());
            }
            if (mySimpleMap.getMediumType() != null) {
                result = result && value.getMediumType().equals(mySimpleMap.getMediumType());
            }
            if (mySimpleMap.getPublicationTime() != null) {
                result = result && value.getPublicationTime().equals(mySimpleMap.getPublicationTime());
            }
            if (mySimpleMap.getCollectionTime() != null) {
                result = result && value.getCollectionTime().equals(mySimpleMap.getCollectionTime());
            }
            return result;
        }).map(Map.Entry::getValue).toArray();
        if (array.length == 0){
            return null;
        }
        return (MySimpleMap) array[0];
    }
}
